package com.student.dao.impl;
//@Classname ParticipantInfoDaoImpl
//@time 17:25
//OMEN

import com.student.dao.ParticipantInfoDao;
import com.student.entity.MedalInfo;
import com.student.entity.ParticipantInfo;
import com.student.util.DBUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ParticipantInfoDaoImpl implements ParticipantInfoDao {
    @Override
    public int insert(ParticipantInfo entity) throws Exception {
        String sql="insert  into participant_info(participan_id,country_code,participan_name,participan_birth,participan_height,participan_kg,participan_img,participan_introduce)values (?,?,?,?,?,?,?,?)";
        try(
                Connection con= DBUtil.getConnection();
                PreparedStatement pstm=con.prepareStatement(sql);
        ) {
            pstm.setObject(1,entity.getParticipanId());
            pstm.setObject(2,entity.getCountryCode());
            pstm.setObject(3,entity.getParticipanName());
            pstm.setObject(4,entity.getParticipanBirth());
            pstm.setObject(5,entity.getParticipanHeight());
            pstm.setObject(6,entity.getParticipanKg());
            pstm.setObject(7,entity.getParticipanImg());
            pstm.setObject(8,entity.getParticipanIntroduce());
            return pstm.executeUpdate();

        }
    }

    @Override
    public int updateById(ParticipantInfo entity) throws Exception {
        String sql = "update participant_info set country_code=?,participan_name=?,participan_birth=?,participan_height=?,participan_kg=?,participan_img=?,participan_introduce=?  where participan_id=?";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);

        ) {
            pstm.setObject(1, entity.getCountryCode());
            pstm.setObject(2, entity.getParticipanName());
            pstm.setObject(3, entity.getParticipanBirth());
            pstm.setObject(4, entity.getParticipanHeight());
            pstm.setObject(5, entity.getParticipanKg());
            pstm.setObject(6, entity.getParticipanImg());
            pstm.setObject(7, entity.getParticipanIntroduce());
            pstm.setObject(8, entity.getParticipanId());
            return pstm.executeUpdate();

        }
    }

    @Override
    public int deleteById(Serializable id) throws Exception {
        String sql = "delete from participant_info where participan_id=?";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);

        ) {
            pstm.setObject(1, id);
            return pstm.executeUpdate();
        }
    }

    @Override
    public ParticipantInfo selectById(Serializable id) throws SQLException {
        String sql = "select * from participant_info where participan_id=?";
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            con = DBUtil.getConnection();
            pstm = con.prepareStatement(sql);
            pstm.setObject(1, id);
            rs = pstm.executeQuery();
            if (rs.next()) {
                ParticipantInfo pi=new ParticipantInfo();
                pi.setParticipanId(rs.getInt("participan_id"));
                pi.setCountryCode(rs.getString("country_code"));
                pi.setParticipanName(rs.getString("participan_name"));
                pi.setParticipanBirth(rs.getDate("participan_birth"));
                pi.setParticipanHeight(rs.getInt("participan_height"));
                pi.setParticipanKg(rs.getString("participan_kg"));
                pi.setParticipanKg(rs.getString("participan_kg"));
                pi.setParticipanImg(rs.getString("participan_img"));
                pi.setParticipanIntroduce(rs.getString("participan_introduce"));


                return pi;

            } else {
                throw new SQLException("没有数据");
            }

        } finally {
            DBUtil.close(con, pstm, rs);

        }
    }

    @Override
    public List<ParticipantInfo> select(int index, int len) throws SQLException {
        String sql ="select * from medal_info order by medal_id limit ?,?";
        Connection con=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,index);
            pstm.setObject(2,len);
            rs=pstm.executeQuery();
            List<ParticipantInfo> list=new ArrayList<>();
            while (rs.next()){
                ParticipantInfo pi=new ParticipantInfo();
                pi.setParticipanId(rs.getInt("participan_id"));
                pi.setCountryCode(rs.getString("country_code"));
                pi.setParticipanName(rs.getString("participan_name"));
                pi.setParticipanBirth(rs.getDate("participan_birth"));
                pi.setParticipanHeight(rs.getInt("participan_height"));
                pi.setParticipanKg(rs.getString("participan_kg"));
                pi.setParticipanKg(rs.getString("participan_kg"));
                pi.setParticipanImg(rs.getString("participan_img"));
                pi.setParticipanIntroduce(rs.getString("participan_introduce"));

                list.add(pi);
            }
            return list;

        }finally {
            DBUtil.close(con,pstm,rs);
        }
    }

    @Override
    public long count() throws SQLException {
        return 0;
    }
}
